//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,//判断小程序的API，回调，参数，组件等是否在当前版本可用。
    isHide: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },

  onLoad(){
    console.log(app.globalData.userInfo)
    wx.showLoading({
      title: '加载中...',
    })
    this.loadBanner()
  },

  onShow: function () {
    this.loadBanner()
    this.volunteer()
  },

  onHide(){
    if(app.globalData.if_auth == 0){
      this.setData({
        isHide:false
      })
    }
  },

  getUserInfo: function (e) {
    // 用户点击允许
    console.log(e)
    console.log(app.globalData.if_auth)
    
    if (e.detail.userInfo) {
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        isHide: false
      })
      let { encryptedData, iv, rawData, signature } = e.detail
      let access_token = wx.getStorageSync('access_token')
      wx.request({
        url: 'https://baokang.wshoto.com/wx/auth',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          encryptedData: encryptedData,
          iv: iv,
          rawData: rawData,
          signature: signature,
          access_token: access_token
        },
        success: (res) => {
          console.log(res)
          wx.setStorageSync('userinfo', res.data.info)
          app.globalData.userInfo = e.detail.userInfo
          app.globalData.if_auth = 1
        }
      })
    } else {
      console.log('用户点击拒绝授权')
    }
  },

  toYiqing() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../yiqing/yiqing',
      })
    }
  },

  toLaw(){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../law/law',
      })
    }
  },

  toGrid(){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../grid/grid',
      })
    }
  },
  
  toSaohei(){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../saohei/saohei',
      })
    }
  },

  toCamera(){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../camera/camera',
      })
    }
  },

  more(){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../volunteer/volunteer',
      })
    }
  },

  // 轮播图
  loadBanner(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/bannerlist',
      data:{
        access_token:access_token,
        location:'zhili'
      },
      success:(res)=>{
        console.log(res.data)
        if(res.data.code >= 500){
          app.getLogin().then(function(){
            console.log('二次登录。。。')
          })
        }
        else if(res.data.code == 1){
          this.setData({
            bannerList:res.data.list
          })
        } 
      },
      complete:(res)=>{
        wx.hideLoading()
      }
    })
  },

  volunteer(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/volunteerlist',
      data:{
        access_token:access_token,
        volunteer_num:3
      },
      success:(res)=>{
        console.log(res)
        if(res.data.code == 1){
          this.setData({
            service_list:res.data.list
          })
        } 
      }
    })
  },

  toVolunteer(e){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      let service_id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '../volunteer-details/volunteer-details?service_id=' + service_id,
      })
    }
  }
})
